package Test;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.PrintStream;
import Main.Compilator;


public class TestRunner 
{
	protected Compilator m_oCompil 	= new Compilator();
	
	public void setRunVM(boolean runVM)
	{
		this.m_oCompil.setRunVM(runVM);
	}
	
	public boolean runVM()
	{
		return this.m_oCompil.runVM();
	}
	
	public void setShowGeneratedCode(boolean b)
	{
		this.m_oCompil.setShowGeneratedCode(b);
	}
	
	public boolean isGeneratedCodeShown()
	{
		return this.m_oCompil.isGeneratedCodeShown();
	}
	
	/**
	 * @return the m_bTypeOnly
	 */
	public boolean isTypeOnly() 
	{
		return this.m_oCompil.isTypeOnly();
	}

	/**
	 * @param typeOnly the m_bTypeOnly to set
	 */
	public void setTypeOnly(boolean typeOnly) 
	{
		this.m_oCompil.setTypeOnly(typeOnly);
	}

	/**
	 * @return the m_bParseOnly
	 */
	public boolean IsParseOnly() 
	{
		return this.m_oCompil.IsParseOnly();
	}

	/**
	 * @param parseOnly the m_bParseOnly to set
	 */
	public void setParseOnly(boolean parseOnly) 
	{
		this.m_oCompil.setParseOnly(parseOnly);
	}
	
	public int TestFile(String filename)
	{
		return this.m_oCompil.Compil(filename);		
	}
	
	public void TestAllFile(String sPath)
	{
		File oPath = new File(sPath);
		if(!oPath.isDirectory())
		{
			System.err.println("\nErreur : dossier invalide : " + sPath);
			return;			
		}
		
		File[] oCollection 	= oPath.listFiles();
		int iCptOK 			= 0;
		int iCptCompilKO 	= 0;
		int iCptInternKO 	= 0;
		for(int i = 0; i < oCollection.length; i++)
		{
			if(oCollection[i].getName().endsWith(Compilator.EXTENTION_FICHIER))
			{
				int iStatus	= TestFile(oCollection[i].getAbsolutePath());
				
				System.out.println("Statut de sortie : " + iStatus);
				
				switch(iStatus)
				{
				case Compilator.NORMAL_TERMINAISON :
					iCptOK++;
					break;
					
				case Compilator.COMPIL_ERR :
					iCptCompilKO++;
					break;
					
				case Compilator.INTERNAL_COMPIL_ERR :
					iCptInternKO++;
					break;
				}			
			}
		}
		String sMsg = 	"\n*************************************** Resultats ***************************************\n" +
						"Dossier de test            : " + sPath			+ "\n" +
						"Test OK                    : " + iCptOK 		+ "\n" +
						"Compil error               : " + iCptCompilKO	+ "\n" +
						"Internal error             : " + iCptInternKO 	+ "\n" +
						"*****************************************************************************************\n";
		System.out.println(sMsg);
	}
		
	public TestRunner()
	{
		
	}
	
	public static void main(String[] args) 
	{
		// On redirige la sortie d'erreur, on n'affiche que la sortie normal
    	System.setErr(new PrintStream(new ByteArrayOutputStream(), false));
    	System.setOut(System.out);
    	
		TestRunner oTestRunner = new TestRunner();
		
		/*oTestRunner.setParseOnly(true);
		oTestRunner.TestAllFile("./Test/Syntaxe/TestOK/");
    	oTestRunner.TestAllFile("./Test/Syntaxe/TestKO/");
    	
    	oTestRunner.setParseOnly(false);
    	oTestRunner.setTypeOnly(true);

		oTestRunner.TestAllFile("./Test/Typage/TestOK/");
    	oTestRunner.TestAllFile("./Test/Typage/TestKO/");
    	
    	oTestRunner.TestAllFile("./Test/Typage/JenOK/");
    	oTestRunner.TestAllFile("./Test/Typage/JenKO/");*/
    	
    	oTestRunner.setParseOnly(false);
    	oTestRunner.setTypeOnly(false);
    	oTestRunner.setShowGeneratedCode(false);
    	oTestRunner.setRunVM(true);
    	//oTestRunner.TestAllFile("./Test/ProductionCode/TestProf/");
    	//oTestRunner.TestAllFile("./Test/ProductionCode/TestNous/");
    	
		oTestRunner.TestFile("./Test/test.jmm");
	}
}
